/**
 * This file is a part of qloudgen-sec. 
 * You can redistribute qloudgen-sec and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-sec is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.sec.cas.support.user.services;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import org.qloudgen.sec.cas.support.user.userregistry.UserRegistryDao;

import javax.validation.constraints.NotNull;

/**
 * 
 */
public final class DefaultUsersManagerImpl implements UsersManager{

    private final Logger logger = LoggerFactory.getLogger(DefaultUsersManagerImpl.class);

    /** Instance of dao. */
    @NotNull
    private UserRegistryDao userRegistryDao;

    public DefaultUsersManagerImpl(final UserRegistryDao userRegistryDao) {
        this.userRegistryDao = userRegistryDao;
    }


    public synchronized User delete(final String id) {
        final User r = findUserBy(id);
        if (r == null) {
            return null;
        }
        logger.debug("usersManageImple delete user: {}",r);
        this.userRegistryDao.delete(r);
        logger.debug("usersManageImple delete user end: {}",r);
        return r;
    }

    public User findUserBy(final String id) {
        User r = this.userRegistryDao.findUserById(id);

        try {
            return r == null ? null : r;
        } catch (final Exception e) {
            return r;
        }
    }


    public synchronized User save(final User user) {
    	
        final User r = this.userRegistryDao.save(user);
        return r;
    }
    
    public List<User> getAllUsers() {
        return userRegistryDao.load();
    }
    
    public User authenticationUserBy(String username,String password){
    	
    	  return userRegistryDao.findUserBy(username,password);
    	
    }
    
    public boolean checkUserIsManagementAdmin(User user){
    	
    	 if (user == null)
    	    return false;
    	 else
    	  return user.getIsManageUser();
    }
    
    public User findUserByName(String username){
    	
    	  return userRegistryDao.findUserByName(username);
    }
}
